package com.wiloon.java8x.io.socket;

/**
 * Created by roy.wang on 2017/4/19;
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class StreamTool {
    public static void main(String[] args) {
        try {
            File file = new File("C:\\ceshi.txt");
            FileInputStream fin = new FileInputStream(file);
            byte[] filebt = readStream(fin);
            System.out.println(filebt.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static byte[] readStream(InputStream inStream) throws Exception {
//        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
//        byte[] buffer = new byte[1024];
//        int len = -1;
//        while ((len = inStream.read(buffer)) != -1) {
//            outSteam.write(buffer, 0, len);
//        }
//        outSteam.close();
//        inStream.close();
//        return outSteam.toByteArray();
//    }

    public static byte[] readStream(InputStream inStream) throws Exception {
        int count = 0;
        while (count == 0) {
            count = inStream.available();
            if (count > 0) System.out.println(count);
        }
        byte[] b = new byte[count];
        inStream.read(b);
        return b;
    }
}